package org.syh.boyangfighthealthclub.controller;

import org.syh.boyangfighthealthclub.service.IUserService;
import org.syh.boyangfighthealthclub.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: Ben
 * @Date: 2021/3/10 21:51
 */
@WebServlet("/userLogin")
public class UserLogin extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        res.setContentType("text/html;utf-8");
        String nickName=req.getParameter("nickName");
        String password=req.getParameter("password");
        String authCode=req.getParameter("authcode");
        HttpSession session=req.getSession();
        String randStrLogin=(String) session.getAttribute("randStrLoginUser");
        System.out.println(authCode);
        System.out.println(randStrLogin);
        if (authCode.equals(randStrLogin)) {
            IUserService iUserService = new UserServiceImpl();
            boolean judgment = iUserService.loginUser(nickName, password);
            if (judgment) {
                session.setAttribute("nickNameUser", nickName);
                req.getRequestDispatcher("user/selectBookUser?page=1").forward(req, res);
                return;
            }
        }
        req.setAttribute("msg","用户登录失败");
        res.sendRedirect("UserIndex.jsp");
    }
}